package com.bs.visitservice.controller.seller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bs.visitservice.entity.OrderInfo;
import com.bs.visitservice.entity.ProductType;
import com.bs.visitservice.entity.ReceiverAddress;
import com.bs.visitservice.entity.Seller;
import com.bs.visitservice.service.*;
import com.bs.visitservice.utils.ParamUtils;
import com.bs.visitservice.utils.Result;
import com.bs.visitservice.utils.ResultUtil;
import com.bs.visitservice.utils.ThreadLocalUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商家订单查询
 */
@RestController
@RequestMapping("/seller")
public class SellerOrderInfoController {

    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private ProductService productService;
    @Autowired
    private SellerService sellerService;
    @Autowired
    private ReceiverAddressService receiverAddressService;
    @Autowired
    private DiscountCouponService discountCouponService;
    @Autowired
    private ProductTypeService productTypeService;
    @RequestMapping(value = "/orderInfoList",method = RequestMethod.POST)
    public Result queryOrderInfo(@RequestBody Map cond){
        Long userNo =(Long) ThreadLocalUtil.get("loginNo");
        LambdaQueryWrapper<Seller> wrapperSeller = new LambdaQueryWrapper<>();
        wrapperSeller.eq(Seller::getLoginNo,userNo).ne(Seller::getStatus,4);
        //暂时只支持一个账号开一家店铺
        List<Seller> sellerList = sellerService.list(wrapperSeller);
        if(sellerList.size() < 1){
            return Result.success(new ArrayList<Map<String,Object>>());
        }
        Seller seller = sellerList.get(0);
        OrderInfo param = (OrderInfo)ResultUtil.transObject(cond,OrderInfo.class);
        IPage page = ParamUtils.getPage(cond);
        LambdaQueryWrapper<OrderInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderInfo::getSellerNo, seller.getSellerNo());
        //通过订单编号查询
        if(!StringUtils.isEmpty(param.getOrderNo())){
            wrapper.eq(OrderInfo::getOrderNo,param.getOrderNo());
        }
        Map<String,Object> result = new HashMap<>();
        wrapper.orderByDesc(OrderInfo::getDatatime);
        page = orderInfoService.pageMaps(page,wrapper);
        List<Map<String, Object>> res = page.getRecords();
        res.forEach(map->{
            Long orderNo = (Long)map.get("no");
            //商品列表
            List<Map<String,Object>> proList = productService.queryOrderProduct(orderNo);
            //用户对订单的逻辑删除在商家端依然是已完成状态 7已评价
            if(7 == (Integer)map.get("orderStatus")){
                map.put("orderStatus",3);
            }
            ResultUtil.transfort(proList, getDictMap(), true);
            map.put("proList", proList);
            //顾客地址
            ReceiverAddress receiveNo = receiverAddressService.getById((Long) map.get("receiveNo"));
            map.put("receiveInfo",receiveNo);
            //订单使用优惠券
            List<Map<String, Object>> discountList = discountCouponService.queryByOrder(orderNo);
            ResultUtil.transfort(discountList,this.getDictMap(),true);
            map.put("disList",discountList);
        });
        ResultUtil.transfort(res, getDictMap(), true);
        result.put("total",page.getTotal());
        result.put("datas",res);
        return Result.success(result);
    }

    /**
     * 更新订单
     * 0 接单中，1 已接单，2已到达，3 已完成
     * @param cond
     * @return
     */
    @RequestMapping(value = "/updateOrderInfo",method = RequestMethod.POST)
    public Result deleteOrderInfo(@RequestBody Map cond){
        OrderInfo orderInfo = (OrderInfo)ResultUtil.transObject(cond,OrderInfo.class);
        boolean isSuc = orderInfoService.updateById(orderInfo);
        if(isSuc){
            return Result.success(null);
        }
        return Result.error(null);
    }
    /**
     * 获取列和字典值的映射关系
     * @return
     */
    private Map getDictMap(){
        Map<String,String> dictMap = new HashMap<>();
        dictMap.put("orderStatus","orderStatus");
        dictMap.put("type","discountType");
        return dictMap;
    }

    /**
     * 查询销售信息
     * @param cond
     * @return
     */
    @RequestMapping(value = "getSellInfo",method = RequestMethod.POST)
    public Result getSellInfo(@RequestBody Map cond){
        if(cond.get("pageIndex")!=null && cond.get("pageSize")!=null){
            Integer pageIndex = (Integer)cond.get("pageIndex");
            Integer pageSize = (Integer)cond.get("pageSize");
            cond.put("pageIndex",(pageIndex-1)*pageSize);
        }
        List<Map<String, Object>> maps = orderInfoService.sellInfo(cond);
        List<Map<String,Object>> datas1 = new ArrayList<>();
        maps.forEach(map ->{
            Map<String,Object> tmap = new HashMap<>();
            tmap.putAll(map);
            Object obj = map.get("productType");
            if(obj != null){
                Long productType = (Long)obj;
                ProductType byId = productTypeService.getById(productType);
                tmap.put("productType",byId.getTypeName());
                tmap.put("productType_ori",productType);
            }
            datas1.add(tmap);
        }) ;
        cond.remove("pageIndex");
        Integer total = orderInfoService.sellInfo(cond).size();
        Map<String,Object> result = new HashMap<>();
        result.put("total",total);
        result.put("datas",datas1);
        return Result.success(result);
    }
}
